package com.chrray.position.util.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chrray.position.pojo.dto.util.WxPhoneVo;
import com.chrray.position.util.http.Httputil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class WXutil {


    public static String getXCXAuth(String appid, String secret, String code) throws IOException {


        String getOpenIdUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";


        RestTemplate re = new RestTemplate();

        HttpEntity<String> jsonObject = re.getForEntity(getOpenIdUrl, String.class);
        log.info("调用微信登录：{}",jsonObject.getBody());
        return jsonObject.getBody();
    }

    public static String  getPhoneAccessToken(String appid, String secret){

        String getOpenIdUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret ;


        RestTemplate re = new RestTemplate();

        HttpEntity<String> jsonObject = re.getForEntity(getOpenIdUrl, String.class);
        log.info("调用微信accesstoken：{}",jsonObject.getBody());
        return  JSONObject.parseObject(jsonObject.getBody()).getString("access_token");
    }

    public static String  getPhone(String code,String accessToken){

        String getOpenIdUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken ;


        RestTemplate re = new RestTemplate();
        WxPhoneVo vo=new WxPhoneVo();
        vo.setCode(code);
        HttpEntity<String> jsonObject = re.postForEntity(getOpenIdUrl,vo, String.class);
        log.info("获取手机号结果：{}",jsonObject.getBody());
        String phone_info = JSONObject.parseObject(jsonObject.getBody()).getString("phone_info");
        log.info("获取手机号结果1：{}",phone_info);
        return  JSONObject.parseObject(phone_info).getString("phoneNumber");
    }

    public static Map getXCXAuthMap(String appid, String secret, String code) throws IOException {
        return (Map) JSON.parse(getXCXAuth(appid, secret, code));
    }

    public static String getXCXAuthOpenid(String appid, String secret, String code) throws IOException {
        return (String) getXCXAuthMap(appid, secret, code).get("openid");
    }

    public static String getXCXAuthSessionKey(String appid, String secret, String code) throws IOException {
        return (String) getXCXAuthMap(appid, secret, code).get("session_key");
    }


    public static String getSapiTicket(String accessToken) throws IOException {

        String getSapiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi";

        String s = Httputil.httpget(getSapiTicketUrl, null);
        return s;
    }

    public static String refreshToken(String appid, String refresh_token) throws IOException {
        String getaccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&refresh_token=" + refresh_token + "&grant_type=refresh_token";
        String s = Httputil.httpget(getaccessTokenUrl, null);
        return s;
    }

    public static String getFwUserInfo(String accessToken, String openid) throws IOException {
        String getaccessTokenUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("Content-Type", "application/json; charset=utf-8");
        String s = Httputil.httpget(getaccessTokenUrl, map2);
        return s;
    }

    public static String getFwOpenid(String appid, String secret, String code) throws IOException {
        String getaccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
        String s = Httputil.httpget(getaccessTokenUrl, null);
        return s;
    }

    public static String accessToken(String appid, String secret) throws IOException {
        String getaccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?appid=" + appid + "&secret=" + secret + "&grant_type=client_credential";
        String s = Httputil.httpget(getaccessTokenUrl, null);
        return s;
    }

    public static String getAccessToken(String appid, String secret, String code) throws IOException {

        String response1 = null;
        System.out.println(code);
        String getOpenIdUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
        //获取返回的code
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(getOpenIdUrl);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        response1 = httpClient.execute(httpGet, responseHandler);
        return response1;
    }

    public static Map getXCXAuthMap2(String appid, String secret, String code) throws IOException {
        return (Map) JSON.parse(getAccessToken(appid, secret, code));
    }


}
